//
//  IGRootTabBarViewController.m
//  jingdongfang
//
//  Created by BOE on 2017/8/15.
//  Copyright © 2017年 kevinLY. All rights reserved.
//

#import "IGRootTabBarViewController.h"
#import "HomeViewController.h"
#import "FindViewController.h"
#import "IGCommunityVC.h"
#import "MeViewController.h"
#import "IGPreIssueView.h"
#import "IGIssueTopicVC.h"
#import "IGMyArtViewController.h"
#import "IGComActivityVC.h"
#import "UpPrintVC.h"
#import "UpOpenViewController.h"
#import "BatchUploadVC.h"
#import "PhotoUpLoadAgreementViewController.h"
#import "UserUNidentyViewController.h"
@interface IGRootTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) IGPreIssueView *issueView;
@property (nonatomic, strong) BaseNavigationController *centerNav;

@end

@implementation IGRootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configControllers];
    [self configTabBar];
    
    self.delegate = self;
}

- (IGPreIssueView *)issueView
{
    @KWeakObj(self)
    if (!_issueView) {
        _issueView = [[IGPreIssueView alloc] init];
        /** 发布公开图片 */
        _issueView.pPhotoControlBlock = ^ {
            //1 未认证, 2 已认证,3 待认证,4 认证未通过
            if ([[IGUser getAuthens] isEqualToString:@"3"]) {//待认证
                [selfWeak presentMessageTips:NSLocalizedString(@"ID under Verification", nil) ];
            } else if([[IGUser getAuthens] isEqualToString:@"2"]) {//通过
                [IGPermissionUtil ig_showActionSheetWithCameraBlock:^{
                    [IGTakePhotosUtil ig_showImagePickerControllerWithImagePickerBlock:^(NSDictionary<NSString *,id> * _Nullable info) {
                        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                        image = [image fixOrientation];
                        
                        UpPrintVC *vc = [[UpPrintVC alloc] initWithImage:image];
                        vc.isOpen = YES;
                        [UIViewController ig_pushViewController:vc animated:YES];
                    }];
                } photoBlock:^{
                    [IGTakePhotosUtil ig_showAssetsForProductPickerControllerWithMaximumNumberOfSelection:1 assetsPickerBlock:^(NSArray<UIImage *> * _Nullable photos, NSArray * _Nullable assets) {
                        if (photos.count != 0) {
                            UIImage *image = photos[0];
                            UpPrintVC *vc = [[UpPrintVC alloc] initWithImage:image];
                            vc.assets = assets;
                            vc.isOpen = YES;
                            [UIViewController ig_pushViewController:vc animated:YES];
                        }
                    }];
                }];
            }
            else
            {
                if ([[IGUser getAuthens] isEqualToString:@"1"] || ![IGUser getAuthens])
                {//未认证
                    PhotoUpLoadAgreementViewController *photoAgreenmentVC = [[UIStoryboard storyboardWithName:@"UserCertification" bundle:nil] instantiateViewControllerWithIdentifier:@"PhotoUpLoadAgreementViewController"];
                    [UIViewController ig_pushViewController:photoAgreenmentVC animated:YES];
                }
                else if([[IGUser getAuthens] isEqualToString:@"4"])
                {//不通过
                    UserUNidentyViewController *vc=[[UserUNidentyViewController alloc]initWithNibName:@"UserUNidentyViewController" bundle:nil];
                    vc.content = [IGUser getAuthensContent];
                    [UIViewController ig_pushViewController:vc animated:YES];
                }
                
            }
            
        };
        
        /** 发布私密图片 */
        _issueView.sPhotoControlBlock = ^ {
            [IGPermissionUtil ig_showActionSheetWithCameraBlock:^{
                [IGTakePhotosUtil ig_showImagePickerControllerWithImagePickerBlock:^(NSDictionary<NSString *,id> * _Nullable info) {
                    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                    image = [image fixOrientation];
                    
                    UpPrintVC *vc = [[UpPrintVC alloc] initWithImage:image];
                    vc.isOpen = NO;
                    [UIViewController ig_pushViewController:vc animated:YES];
                }];
            } photoBlock:^{
                [IGTakePhotosUtil ig_showAssetsForProductPickerControllerWithMaximumNumberOfSelection:8 assetsPickerBlock:^(NSArray<UIImage *> * _Nullable photos, NSArray * _Nullable assets)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //选择一张时进入裁剪界面
                        if (photos.count == 1)
                        {
                            UIImage *image = photos[0];
                            UpPrintVC *vc = [[UpPrintVC alloc] initWithImage:image];
                            vc.assets = assets;
                            vc.isOpen = NO;
                            [UIViewController ig_pushViewController:vc animated:YES];
                        }
                        //选则多张时进入批量上传界面
                        else if (photos.count > 1)
                        {
                            BatchUploadVC *vc = [[BatchUploadVC alloc] init];
                            vc.assets = [NSMutableArray arrayWithArray:assets];
                            vc.images = [NSMutableArray arrayWithArray:photos];
                            [UIViewController ig_pushViewController:vc animated:YES];
                        }
                    });
                }];
            }];
            
        };
        
        /** 发布话题 */
        _issueView.topicControlBlock = ^ {
            [UIViewController ig_pushViewControllerClass:[IGIssueTopicVC class] animated:YES];
        };
    }
    return _issueView;
}

- (void)configControllers
{
    /** 主页 */
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    BaseNavigationController *navHome = [[BaseNavigationController alloc] initWithRootViewController:homeVC];
    
    /** 我的 */
//    FindViewController *findVC = [[FindViewController alloc] init];
//    BaseNavigationController *navFind = [[BaseNavigationController alloc]initWithRootViewController:findVC];
    IGMyArtViewController *myArtVC=[[IGMyArtViewController alloc]init];
    BaseNavigationController *navMyArt = [[BaseNavigationController alloc]initWithRootViewController:myArtVC];
    
    /** + */
    UIViewController *centerVC = [[UIViewController alloc] init];
    _centerNav = [[BaseNavigationController alloc]initWithRootViewController:centerVC];
    
    /** 圈子 */
    IGComActivityVC *communityVC = [[IGComActivityVC alloc] init];
    BaseNavigationController *navCommunity = [[BaseNavigationController alloc]initWithRootViewController:communityVC];

    /** 账号 */
    MeViewController *meVC = [[MeViewController alloc] init];
    BaseNavigationController *navMe = [[BaseNavigationController alloc]initWithRootViewController:meVC];

    self.viewControllers = @[navHome,navMyArt, _centerNav, navCommunity, navMe];
    [self setSelectedIndex:0];
}

- (void)configTabBar
{
    NSArray *titles = @[@"Discover", @"我的", @"", @"Group", @"账号"];
    
    NSArray *normalImg = @[@"home_normal", @"find_normal", @"pub_normal", @"community_normal", @"mespace_normal"];
    NSArray *selectedImg = @[@"home_selected", @"find_selected", @"pub_normal", @"community_selected", @"mespace_selected"];
    NSInteger count = titles.count;
    
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont font10];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor c5Color];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = [UIFont font10];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor appColor];
    
    for (NSInteger index = 0; index < count; index++) {
        UITabBarItem *item = [[UITabBarItem alloc]
                              initWithTitle:NSLocalizedString(titles[index], nil)
                              image:[[UIImage imageNamed:normalImg[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                              selectedImage:[[UIImage imageNamed:selectedImg[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
        [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
        if (index == 2) {
            item.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
        }
        UIViewController *vc = [self.viewControllers objectAtIndex:index];
        vc.tabBarItem = item;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController == _centerNav) {
        /** 发布页面：私密图片、公开图片、话题 */
        [self.view addSubview:self.issueView];
        [self.issueView show];
        
        return NO;
    }
    return YES;
}

// 是否支持自动转屏
- (BOOL)shouldAutorotate
{
    UINavigationController *nav = self.viewControllers[self.selectedIndex];
    return [nav.topViewController shouldAutorotate];
}

// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    UINavigationController *nav = self.viewControllers[self.selectedIndex];
    return [nav.topViewController supportedInterfaceOrientations];
}

// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UINavigationController *nav = self.viewControllers[self.selectedIndex];
    return [nav.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
